package cn.nnb.control.cy;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.snow.order.biz.FoodBiz;
import com.snow.order.entity.Food;
import com.snow.order.log.Log;

public class foodEdit extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public foodEdit() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

//		response.setContentType("text/html;charset=utf-8");
		//String path=null;
		int fid =Integer.parseInt(request.getParameter("fid"));
		String fna = request.getParameter("fna");
	   String path = request.getParameter("path");
		
	   float fpr = Float.parseFloat(request.getParameter("fpr"));
		float fdis = Float.parseFloat(request.getParameter("fdis"));
		Food food=new Food(fid,fna,fpr,fdis);
		FoodBiz biz=new FoodBiz();
		String pathfail="pathfail";
		File file =new File(path);
		
		if(path.length()==0){
			try{
				String success="success";
				//System.out.println("patnpathpath"+path.length());
	        	 biz.updateFood(path, food);
	        	
						request.setAttribute("food", food);
						request.setAttribute("fid",fid);
						request.setAttribute("fna", fna);
						request.setAttribute("fpr", fpr);
						request.setAttribute("fdis", fdis);
						request.setAttribute("success", success);
						
						request.getRequestDispatcher("cy/FoodEdit.jsp").forward(request, response);
					
				} catch (Exception e) {
					Log.logger.error(e.getMessage());
					request.setAttribute("errMsg","閺堝秴濮熼崳銊х畳韫囨瑣锟介妴鍌橈拷");
					request.getRequestDispatcher("/error.jsp").forward(request, response);
				}			
		}
			
		
		else if(path.length()<7){
			request.setAttribute("foodlist", food);
			request.setAttribute("fid",fid);
			request.setAttribute("fna", fna);
			request.setAttribute("fpr", fpr);
			request.setAttribute("fdis", fdis);
		
			request.setAttribute("pathfail", pathfail);
			
			request.getRequestDispatcher("cy/FoodEdit.jsp").forward(request, response);
		}
		else if(! file.exists()||!path.substring(path.length()-4,path.length()).equals(".jpg")){//閸掋倖鏌囬弬鍥︽(婢讹拷閺勵垰鎯佺�妯烘躬
			//System.out.println("exist"+file.exists());
			request.setAttribute("foodlist", food);
			request.setAttribute("fid",fid);
			request.setAttribute("fna", fna);
			request.setAttribute("fpr", fpr);
			request.setAttribute("fdis", fdis);
		
			request.setAttribute("pathfail", pathfail);
			
			request.getRequestDispatcher("cy/FoodEdit.jsp").forward(request, response);
		}
		else{
         try{
			String success="success";
			//System.out.println("patnpathpath"+path.length());
        	 biz.updateFood(path, food);
        	
					request.setAttribute("foodlist", food);
					request.setAttribute("fid",fid);
					request.setAttribute("fna", fna);
					request.setAttribute("fpr", fpr);
					request.setAttribute("fdis", fdis);
					request.setAttribute("success", success);
					
					request.getRequestDispatcher("cy/FoodEdit.jsp").forward(request, response);
				
			} catch (Exception e) {
				Log.logger.error(e.getMessage());
				request.setAttribute("errMsg","閺堝秴濮熼崳銊х畳韫囨瑣锟介妴鍌橈拷");
				request.getRequestDispatcher("/error.jsp").forward(request, response);
			}			
	}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
